package com.fz.us.admin.dict.service.impl;

import com.fz.us.admin.base.bean.Result;
import com.fz.us.admin.base.dao.BaseDao;
import com.fz.us.admin.base.service.impl.BaseServiceImpl;
import com.fz.us.admin.dict.dao.DictItemSettingDao;
import com.fz.us.admin.dict.entity.DictItem;
import com.fz.us.admin.dict.entity.DictItemSetting;
import com.fz.us.admin.dict.service.DictItemSettingService;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;

/**
 * Service实现类 - 字典 item
 * ============================================================================
 * 版权所有 2016 。
 *
 * @author fallenpanda
 * @version 1.0 2016-07-19 。
 * ============================================================================
 */
@Service
public class DictItemSettingServiceImpl extends BaseServiceImpl<DictItemSetting,String> implements DictItemSettingService {

    @Resource
    private DictItemSettingDao dictItemSettingDao;

    @Override
    public BaseDao<DictItemSetting, String> getBaseDao() {
        return dictItemSettingDao;
    }

    @Override
    public boolean isExistSetting(String keyId, String dictitemId) {
        return dictItemSettingDao.isExistSetting(keyId, dictitemId);
    }

    @Override
    public DictItemSetting getSetting(String keyId, String dictitemId) {
        return dictItemSettingDao.getSetting(keyId, dictitemId);
    }

    @Override
    public String getDefinedName(String keyId, String dictitemId) {
        return dictItemSettingDao.getDefinedName(keyId, dictitemId);
    }

    @Override
    public Result createDefined(String keyId, String dictId, String name) {
        Assert.hasText(keyId, "keyId is required");
        Assert.hasText(dictId, "dictId is required");

        DictItemSetting dictItemSetting = new DictItemSetting(keyId, dictId, name);
        return  saveEntity(dictItemSetting);
    }

    @Override
    public Result editDefined(String id, String name) {
        Assert.hasText(id, "id is required");
        Assert.hasText(name, "name is required");

        DictItemSetting dictItemSettingTemp = get(id);
        dictItemSettingTemp.setName(name);

        update(dictItemSettingTemp);

        return resultService.successWithId(dictItemSettingTemp.getId());
    }

}
